<!-- 客户登录后的预定记录页面 mybookings.php-->
<!DOCTYPE html>
<html>
<?php
session_start();
require 'connection.php';
$conn = Connect();
?>

<head>
    <link rel="stylesheet" href="assets/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="assets/css/customerlogin.css">
    <script type="text/javascript" src="assets/js/jquery.min.js"></script>
    <script type="text/javascript" src="assets/js/bootstrap.min.js"></script>
    <link rel="stylesheet" type="text/css" media="screen" href="assets/css/clientpage.css" />
</head>

<body id="page-top" data-spy="scroll" data-target=".navbar-fixed-top">
    <!-- 导航栏 -->
    <?php include './nav.php'; ?>

    <?php
    // 获取当前用户登录信息
    $login_customer = $_SESSION['login_customer'];

    // 每页显示的记录数
    $records_per_page = 2;

    // 获取当前页码
    $page = isset($_GET['page']) ? $_GET['page'] : 1;

    // 查询当前用户已归还的租赁汽车信息总数
    $sqlCount = "SELECT COUNT(*) as total FROM rentedcars rc, cars c 
         WHERE rc.customer_username='$login_customer' AND c.car_id=rc.car_id AND rc.return_status='R'";
    $resultCount = $conn->query($sqlCount);
    $rowCount = $resultCount->fetch_assoc();
    $total_records = $rowCount['total'];

    // 计算总页数
    $total_pages = ceil($total_records / $records_per_page);

    // 计算当前页的起始记录
    $start_record = ($page - 1) * $records_per_page;

    // 查询当前用户已归还的租赁汽车信息
    $sql1 = "SELECT * FROM rentedcars rc, cars c 
         WHERE rc.customer_username='$login_customer' AND c.car_id=rc.car_id AND rc.return_status='R'
         LIMIT $start_record, $records_per_page";
    $result1 = $conn->query($sql1);
    ?>

    <div class="container">
        <?php
        // 如果存在已归还的租赁汽车信息，则显示以下内容
        if (mysqli_num_rows($result1) > 0) {
        ?>
            <div class="jumbotron">
                <h1 class="text-center">您的预订</h1>
                <p class="text-center">希望您享受我们的服务</p>
            </div>

            <div class="table-responsive" style="padding-left: 6.25rem; padding-right: 6.25rem;">
                <table class="table table-striped">
                    <thead class="thead-dark">
                        <tr>
                            <th width="15%">车辆</th>
                            <th width="15%">开始日期</th>
                            <th width="15%">结束日期</th>
                            <th width="10%">费用</th>
                            <th width="15%">距离（公里）</th>
                            <th width="15%">天数</th>
                            <th width="15%">总金额</th>
                        </tr>
                    </thead>
                    <?php
                    // 循环显示用户的每辆已归还的租赁汽车信息
                    while ($row = mysqli_fetch_assoc($result1)) {
                    ?>
                        <tr>
                            <td><?php echo $row["car_name"]; ?></td>
                            <td><?php echo $row["rent_start_date"] ?></td>
                            <td><?php echo $row["rent_end_date"]; ?></td>
                            <td>Rs. <?php
                                    // 根据计费类型显示相应的费用信息
                                    if ($row["charge_type"] == "days") {
                                        echo ($row["fare"] . "/天");
                                    } else {
                                        echo ($row["fare"] . "/公里");
                                    }
                                    ?></td>
                            <td><?php
                                // 根据计费类型显示相应的距离信息或占位符
                                if ($row["charge_type"] == "days") {
                                    echo ("-");
                                } else {
                                    echo ($row["distance"]);
                                }
                                ?></td>
                            <td><?php echo $row["no_of_days"]; ?> </td>
                            <td>Rs. <?php echo $row["total_amount"]; ?></td>
                        </tr>
                    <?php } ?>
                </table>
            </div>

            <!-- 分页链接 -->
            <div class="container">
                <div class="text-center">
                    <nav aria-label="Page navigation ">
                        <ul class="pagination">

                            <?php
                            // 添加上一页链接
                            $prev_page = ($page > 1) ? $page - 1 : 1;
                            $prev_disabled = ($page == 1) ? 'disabled' : ''; // 如果是第一页，禁用上一页按钮
                            echo "<li class='page-item $prev_disabled'><a class='page-link' href='mybookings.php?page=$prev_page'>上一页</a></li>";

                            // 显示分页链接
                            for ($i = 1; $i <= $total_pages; $i++) {
                                $activeClass = ($i == $page) ? 'active' : '';
                                echo "<li class='page-item $activeClass'><a class='page-link' href='mybookings.php?page=$i'>$i</a></li>";
                            }

                            // 添加下一页链接
                            $next_page = ($page < $total_pages) ? $page + 1 : $total_pages;
                            $next_disabled = ($page == $total_pages) ? 'disabled' : ''; // 如果是最后一页，禁用下一页按钮
                            echo "<li class='page-item $next_disabled'><a class='page-link' href='mybookings.php?page=$next_page'>下一页</a></li>";
                            ?>
                        </ul>
                    </nav>
                </div>
            </div>

        <?php
        } else {
            // 如果不存在已归还的租赁汽车信息，则显示以下内容
        ?>
            <div class="jumbotron">
                <h1 class="text-center">您目前还没有租车！</h1>
                <p class="text-center">请租车以查看您的数据。</p>
            </div>
        <?php } ?>
    </div>

    <footer class="site-footer">
        <div class="container">
            <hr>
            <div class="row">
                <div class="col-sm-6">
                    <h5>© <?php echo date("Y"); ?> 租车服务</h5>
                </div>
            </div>
        </div>
    </footer>

</body>

</html>